package Hot100;

import DMSXL._2023Before.ListNode_.ListNode;

public class mergeTwoLists {
    public static void main(String[] args) {
        int[] listA = {1};
        int[] listB = {1};
        ListNode headA = new ListNode(listA[0]);
        ListNode headB = new ListNode(listB[0]);
        ListNode preA = headA;
        ListNode preB = headB;
        for (int i = 1; i < listA.length; i++) {
            ListNode temp = new ListNode(listA[i]);
            preA.next = temp;
            preA = preA.next;
        }
        for (int i = 1; i < listB.length; i++) {
            ListNode temp = new ListNode(listB[i]);
            preB.next = temp;
            preB = preB.next;
        }
        ListNode C = mergeTwoLists(headA,headB);
        while (C != null){
            System.out.print(C.val  + "  ") ;
            C = C.next;
        }
    }
    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode prehead = new ListNode();
        ListNode prev = prehead;

        while (list1 != null && list2 != null)
        {
            if(list1.val <= list2.val){
                prev.next = list1;
                list1 = list1.next;
            }
            else {
                prev.next = list2;
                list2 = list2.next;
            }
        }
        prev.next = list1==null?list2:list1;
        return prehead.next;


    }
}
